<template>
	<view class="container">
		<tui-navigation-bar :isOpacity="false" :maxOpacity="1" @init="initNavigation" :scrollTop="scrollTop"
			backgroundColor="#fff" color="#333" :title="pageTitle">
			<view class="tui-header" :style="{ marginTop: navBarTop + 'px' }">
				<view class="get-back dy-iconfont icon-qiehuanqizuo" @click="goBack"></view>
			</view>
		</tui-navigation-bar>
		<view class="main">
			<view class="send-content">
				<view class="appraise-tab">
					<view class="tab" :class="{'active':item.id == appraiseStatus.current}"
						v-for="(item,index) in appraiseStatus.list" :key="item.id" @tap="appraiseTab(item)">
						<image class="appraise-icon" :src="item.image" mode=""></image>
						<text>{{item.name}}</text>
					</view>
				</view>
				<uv-textarea v-model="sendValue" placeholder="点击这里输入评价" class="uv-textarea-a"
					placeholderStyle="fontSize: 26rpx;fontSeight: 400;color: #999B9B;lineHeight: 37rpx;" border="none"
					height="150" :maxlength="200"></uv-textarea>
				<view class="add-image-list">
					<uv-upload :fileList="fileList1" name="1" multiple :maxCount="maxCount" accept="image"
						@afterRead="afterRead" @delete="deletePic" @error="uploadError" ref="upload">
						<view class="add-image" @tap="selectMedia">
							<uv-icon name="plus" color="#D8D8D8" size="50"></uv-icon>
						</view>
					</uv-upload>
				</view>
				<view class="login-foot">
					<view class="pdCkeckOut" @tap.stop="onAgreement">
						<mycheckbox :check="agreement"></mycheckbox>
					</view>
					<view @tap="onAgreement">匿名评价</view>
				</view>
			</view>
		</view>
		<button class="btn-primary btn-submit" @tap.stop.prevent="onSubmit">发布</button>
		<survey-pop></survey-pop>
		<uv-toast ref="toast"></uv-toast>
	</view>
</template>

<script>
	import mp from "@/mixins/mp.js";
	import throttle from '@/uni_modules/uv-ui-tools/libs/function/throttle.js';
	const app = getApp();
	export default {
		mixins: [mp],
		options: {
			styleIsolation: 'shared'
		}, //解决/deep/不生效**
		components: {},
		data() {
			return {
				navBarOpacity: 0, //标题图标距离顶部距离
				scrollTop: 0.5, //页面滚动高度
				navBarTop: 0, // 导航栏content顶部m-top
				pageTitle: '发布评价', //标题
				agreement: false,
				sendValue: '',
				pictureList: [],
				fileList1: [],
				maxCount: 9,
				imgLoding: true,
				orderId: null,
				appraiseStatus: {
					list: [{
						image: 'https://pengtou-1322587508.cos.ap-shanghai.myqcloud.com/static/images/community/recommend.png',
						name: '推荐',
						id: 1,
					}, {
						image: 'https://pengtou-1322587508.cos.ap-shanghai.myqcloud.com/static/images/community/no_ recommend.png',
						name: '不推荐',
						id: 0,
					}],
					current: null,
				}
			};
		},
		/**
		 * 计算属性
		 */
		computed: {
			getCountPicture() {
				return this.pictureList.length;
			},
		},
		onLoad(options) {
			this.orderId = options.id
		},
		onShow: function() {},
		/**
		 * 下拉
		 */
		onPullDownRefresh: function() {},
		/**
		 * 触底
		 */
		onReachBottom() {},
		/**
		 * 滚动
		 */
		onPageScroll(res) {
			this.scrollTop = res.scrollTop;
		},
		methods: {
			goLogin() {
				app.doLogin()
			},
			initNavigation(e) {
				this.navBarOpacity = e.opacity;
				this.navBarTop = e.top;
			},

			selectMedia() {
				this.$refs.upload.chooseFile();
			},
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url, lists[i].type)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: result ? 'success' : 'failed',
						message: result ? '' : '上传失败',
						url: !result ? '' : result.coverPictureUrl ? result.coverPictureUrl : result.url,
						path: result ? result.path : '',
						type: result ? result.fileType : '',
						fileType: result ? result.fileType : '',
						fileUrl: result ? result.url : '',
						coverPicturePath: result ? result.coverPicturePath : '',
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url, type) {
				return new Promise((resolve, reject) => {
					let header = {}
					if (app.globalData.token) {
						header = {
							token: 'Bearer ' + app.globalData.token,
						}
					}
					let a = uni.uploadFile({
						url: app.globalData.api_root + '/client/file/anyone/upload',
						filePath: url,
						name: 'file',
						formData: {
							storageType: 'TENCENT_COS',
							bucket: 'pengtou-1322587508',
							bizType: type == 'video' ? 'video-frame' : 'send'
						},
						header: header || {},
						success: (res) => {
							setTimeout(() => {
								this.$refs.toast.hide()
								let result = typeof res.data === "object" ? res.data : JSON
									.parse(res.data);
								if (0 == result.code || 200 == result.code) {
									let time = result.data.coverPictureUrl ? 2000 : 200
									setTimeout(() => {
										resolve(result.data)
									}, time)
								} else {
									setTimeout(() => {
										resolve(false);
									}, 200)
								}
							}, 1000)
						},
						fail: (err) => {
							console.log('err', err);
							setTimeout(() => {
								resolve(false);
							}, 1000)
						},
					});
				})
			},


			appraiseTab(item) {
				if (item.id != this.appraiseStatus.current) {
					this.appraiseStatus.current = item.id
				}
			},
			onAgreement(detail) {
				this.agreement = !this.agreement;
			},
			//文本内容审核  1 资料；2 评论；3 论坛；
			checkMessage(messageContent, scene) {
				return new Promise((resolve, reject) => {
					app._post_form(
						"/client/input-check/check-message", {
							messageContent: messageContent,
							scene: scene
						},
						(result) => {
							let res = result.data
							resolve(res);
						}, null, () => {
							resolve(false);
						}
					);
				});
			},
			async onSubmit() {
				let that = this;
				if (that.appraiseStatus.current == null) {
					uni.showToast({
						title: '请表明您的态度',
						icon: 'none',
						duration: 2000
					});
					return
				}
				if (!that.sendValue) {
					uni.showToast({
						title: '请输入您的评价',
						icon: 'none',
						duration: 2000
					});
					return
				}
				let check_message = await this.checkMessage(that.sendValue, 3)
				if (!check_message) {
					this.$refs.toast.show({
						type: 'default',
						message: "请勿输入违规内容"
					})
					setTimeout(() => {
						this.$refs.toast.hide()
					}, 1000)
					return
				}
				that.$refs.toast.show({
					type: 'loading',
				})
				let sendPictureList = [];
				if (this.fileList1.length > 0) {
					this.fileList1.forEach(item => {
						sendPictureList.push({
							"fileUrl": item.path
						})
					})
				}
				throttle(
					app._post_form("/client/forum/submit", {
							"content": that.sendValue,
							"forumType": 0,
							"recommendState": that.appraiseStatus.current,
							"orderId": that.orderId,
							"anonymityState": that.agreement ? 1 : 0,
							"pictureList": sendPictureList
						},
						(res) => {
							that.$refs.toast.hide()
							setTimeout(() => {
								this.$refs.toast.show({
									type: 'success',
									message: "评价成功"
								})
								setTimeout(() => {
									this.$refs.toast.hide()
									app.navigationTo('page_user/appraise/appraise')
								}, 1000)
							}, 500)
						}, null, () => {
							that.$refs.toast.hide()
						}, () => {
							that.$refs.toast.hide()
						}), 1000, true)
			},
			// 返回上一页
			goBack: function() {
				app.beNavigateBack();
			},
		},
	};
</script>
<style lang="scss" scoped>
	.container {
		min-height: 100vh;
		background: #F6F8F7;

		.pdCkeckOut {
			padding: 15rpx;
			padding-right: 10rpx;
		}

		.tui-header {
			width: 100%;
			position: fixed;
			top: 0;
			padding: 0 42rpx;
			display: flex;
			align-items: center;
			height: 32px;
			transform: translateZ(0);
			z-index: 99999;
			box-sizing: border-box;

			.get-back {
				flex-shrink: 0;
				width: 36rpx;
				height: 36rpx;
				color: #000;
				font-size: 36rpx;
				padding: 2rpx 40rpx 2rpx 2rpx;
			}
		}

		.main {
			box-sizing: border-box;
			padding: 212rpx 24rpx 66rpx;

			.send-content {
				min-height: 566rpx;
				background: #FFFFFF;
				border-radius: 12rpx;
				box-sizing: border-box;
				padding: 36rpx 28rpx 20rpx 28rpx;

				.appraise-tab {
					display: flex;
					justify-content: space-between;
					margin-bottom: 36rpx;

					.tab {
						width: 310rpx;
						height: 88rpx;
						background: #F6F8F7;
						border-radius: 52rpx;
						font-size: 30rpx;
						font-weight: 900;
						color: #282D2D;
						line-height: 88rpx;
						box-sizing: border-box;
						position: relative;
						padding-left: 150rpx;

						&.active {
							background: #A7F517;
						}
					}

					.appraise-icon {
						width: 100rpx;
						height: 100rpx;
						position: absolute;
						left: 0;
						top: -16rpx;
					}
				}

				.uv-textarea-a {
					padding: 0;
				}
			}

			.add-image-list {
				display: flex;
				flex-wrap: wrap;
			}

			.add-image {
				width: 190rpx;
				height: 190rpx;
				background: #F6F8F7;
				border-radius: 16rpx;
				border: 2rpx dashed #C4CFCA;
				display: flex;
				align-items: center;
				justify-content: center;
			}

			.picture-item {
				position: relative;

				.del-btn {
					position: absolute;
					display: flex;
					top: -10rpx;
					right: -10rpx;
					z-index: 2;
				}
			}

			.picture-item-video {
				margin: 0 auto;
			}

			.picture-item-image {
				width: 190rpx;
				height: 190rpx;
				margin-right: 20rpx;
				margin-bottom: 20rpx;

				.picture-image {
					width: 100%;
					height: 100%;
					border-radius: 10px;
				}
			}

			.login-foot {
				font-size: 24rpx;
				font-weight: 400;
				line-height: 44rpx;
				margin-top: 42rpx;
				color: #282D2D;
				display: flex;
				align-items: center;
			}
		}

		.btn-submit {
			position: fixed;
			width: calc(100% - 77rpx * 2);
			bottom: calc(constant(safe-area-inset-bottom) + 64rpx);
			bottom: calc(env(safe-area-inset-bottom) + 64rpx);
			left: 50%;
			transform: translateX(-50%);
			z-index: 10;
		}

		.accompany-loading {
			position: absolute;
			top: 40rpx;
			left: 50%;
			transform: translateX(-50%);
		}
	}
</style>